package com.cnap.db.entity;

import com.cnap.model.schedule.engine.DeployPolicyConfigV2;
import com.cnap.model.schedule.engine.alg.AlgDeployClusterInfo;
import com.cnap.model.strategy.SvcConstants;
import com.cnap.model.strategy.schedule.*;
import com.cnap.utils.CommonUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 调度策略数据库模型
 *
 * @author xc
 **/
public class ScheduleStrategyPO {
    private String id;
    private String serviceId;
    // load 不为null则说明启用
    private LoadStrategyDTO load;
    // energy 不为null则说明启用
    private EnergyStrategyDTO energy;
    // cost 不为null则说明启用
    private CostStrategyDTO cost;
    // sla 不为null则说明启用
    private SLAStrategyDTO sla;
    // 智能决策，1：生效，0：不生效
    private int intelligentScheduleActive;
    // 智能决策周期
    private int intelligentDuration;

    // 用户选择
    private String userChooseCloud;
    // 算法推荐
    private String recommendCloud;
    // 用户选择云账户
    private String userChooseAccountName;
    // 算法推荐云账户
    private String recommendAccountName;
    // 详细信息
    private List<AlgDeployClusterInfo> algDeployClusterInfos;
    // 创建时间
    private LocalDateTime createTime;
    // 业务场景
    private BusinessScene businessScene;
    // 服务模型调度（只针对gpu）
    private ServiceModelDTO serviceModel;

    /**
     * get
     *
     * @return UserChooseAccountName
     */
    public String getUserChooseAccountName() {
        return userChooseAccountName;
    }

    /**
     * set
     *
     * @param userChooseAccountName userChooseAccountName
     */
    public void setUserChooseAccountName(String userChooseAccountName) {
        this.userChooseAccountName = userChooseAccountName;
    }

    /**
     * get
     *
     * @return RecommendAccountName
     */
    public String getRecommendAccountName() {
        return recommendAccountName;
    }

    /**
     * set
     *
     * @param recommendAccountName recommendAccountName
     */
    public void setRecommendAccountName(String recommendAccountName) {
        this.recommendAccountName = recommendAccountName;
    }

    /**
     * get
     *
     * @return ServiceModel
     */
    public ServiceModelDTO getServiceModel() {
        return serviceModel;
    }

    /**
     * set
     *
     * @param serviceModel serviceModel
     */
    public void setServiceModel(ServiceModelDTO serviceModel) {
        this.serviceModel = serviceModel;
    }

    /**
     * get
     *
     * @return BusinessScene
     */
    public BusinessScene getBusinessScene() {
        return businessScene;
    }

    /**
     * set
     *
     * @param businessScene businessScene
     */
    public void setBusinessScene(BusinessScene businessScene) {
        this.businessScene = businessScene;
    }

    /**
     * get
     *
     * @return IntelligentDuration
     */
    public int getIntelligentDuration() {
        return intelligentDuration;
    }

    /**
     * set
     *
     * @param intelligentDuration intelligentDuration
     */
    public void setIntelligentDuration(int intelligentDuration) {
        this.intelligentDuration = intelligentDuration;
    }

    /**
     * get
     *
     * @return CreateTime
     */
    public LocalDateTime getCreateTime() {
        return createTime;
    }

    /**
     * set
     *
     * @param createTime createTime
     */
    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    /**
     * get
     *
     * @return UserChooseCloud
     */
    public String getUserChooseCloud() {
        return userChooseCloud;
    }

    /**
     * set
     *
     * @param userChooseCloud userChooseCloud
     */
    public void setUserChooseCloud(String userChooseCloud) {
        this.userChooseCloud = userChooseCloud;
    }

    /**
     * get
     *
     * @return RecommendCloud
     */
    public String getRecommendCloud() {
        return recommendCloud;
    }

    /**
     * set
     *
     * @param recommendCloud recommendCloud
     */
    public void setRecommendCloud(String recommendCloud) {
        this.recommendCloud = recommendCloud;
    }

    /**
     * get
     *
     * @return AlgDeployClusterInfos
     */
    public List<AlgDeployClusterInfo> getAlgDeployClusterInfos() {
        return algDeployClusterInfos;
    }

    /**
     * set
     *
     * @param algDeployClusterInfos algDeployClusterInfos
     */
    public void setAlgDeployClusterInfos(List<AlgDeployClusterInfo> algDeployClusterInfos) {
        this.algDeployClusterInfos = algDeployClusterInfos;
    }

    /**
     * get
     *
     * @return Id
     */
    public String getId() {
        return id;
    }

    /**
     * set
     *
     * @param id id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * get
     *
     * @return ServiceId
     */
    public String getServiceId() {
        return serviceId;
    }

    /**
     * set
     *
     * @param serviceId serviceId
     */
    public void setServiceId(String serviceId) {
        this.serviceId = serviceId;
    }

    /**
     * get
     *
     * @return Load
     */
    public LoadStrategyDTO getLoad() {
        return load;
    }

    /**
     * set
     *
     * @param load load
     */
    public void setLoad(LoadStrategyDTO load) {
        this.load = load;
    }

    /**
     * get
     *
     * @return Energy
     */
    public EnergyStrategyDTO getEnergy() {
        return energy;
    }

    /**
     * set
     *
     * @param energy energy
     */
    public void setEnergy(EnergyStrategyDTO energy) {
        this.energy = energy;
    }

    /**
     * get
     *
     * @return Cost
     */
    public CostStrategyDTO getCost() {
        return cost;
    }

    /**
     * set
     *
     * @param cost cost
     */
    public void setCost(CostStrategyDTO cost) {
        this.cost = cost;
    }

    /**
     * get
     *
     * @return Sla
     */
    public SLAStrategyDTO getSla() {
        return sla;
    }

    /**
     * set
     *
     * @param sla sla
     */
    public void setSla(SLAStrategyDTO sla) {
        this.sla = sla;
    }

    /**
     * get
     *
     * @return IntelligentScheduleActive
     */
    public int getIntelligentScheduleActive() {
        return intelligentScheduleActive;
    }

    /**
     * set
     *
     * @param intelligentScheduleActive intelligentScheduleActive
     */
    public void setIntelligentScheduleActive(int intelligentScheduleActive) {
        this.intelligentScheduleActive = intelligentScheduleActive;
    }


    /**
     * 转换成vo
     *
     * @return 结果
     */
    public ScheduleStrategyVO toVO() {
        ScheduleStrategyVO vo = new ScheduleStrategyVO();
        vo.setId(this.id);
        vo.setServiceId(this.serviceId);
        vo.setLoad(this.load);
        vo.setPrice(this.cost);
        vo.setEnergy(this.energy);
        vo.setSla(this.sla);
        vo.setIntelligentScheduleActive(this.intelligentScheduleActive);
        vo.setIntelligentDuration(this.intelligentDuration);
        vo.setRecommendCloud(this.recommendCloud);
        vo.setUserChooseCloud(this.userChooseCloud);
        vo.setAlgDeployClusterInfos(this.algDeployClusterInfos);
        vo.setCreateTime(CommonUtils.getTimestampFromLocalDateTime(this.createTime));

        vo.setUserChooseAccountName(this.userChooseAccountName);
        vo.setRecommendAccountName(this.recommendAccountName);

        vo.setBusinessScene(this.businessScene);
        return vo;
    }

    /**
     * 转换成请求配置
     *
     * @return 结果
     */
    public List<DeployPolicyConfigV2> toDeployPolicyConfigV2s() {
        List<CommonScheduleDTO> dtos = Arrays.asList(this.load, this.energy, this.cost, this.sla);

        return dtos.stream().filter(Objects::nonNull).map(CommonScheduleDTO::toConfigV2).filter(Objects::nonNull).collect(Collectors.toList());
    }

    /**
     * 获取CPU还是GPU
     *
     * @return 结果
     */
    public String checkArch() {
        if (this.getBusinessScene().getSvcType().equals(SvcTypeEnum.AI_TRAIN.getSvcType())) {
            return SvcConstants.GPU;
        } else {
            return SvcConstants.CPU;
        }
    }
}
